const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

function http({
  url,
  success
}){
  wx.request({
    url,
    header:{"content-type":"application/json"},
    method:"GET",
    responseType:"text",
    success:res=>{
      success(res)
    }
  })
}

function handleStar(num){
  var res = [];
  for(var i = 0 ; i < 5 ; i++){
    if(num>=2){
        res.push(2);
    }else if(num>0){
        res.push(parseFloat(num.toFixed(1)));
    }else{
        res.push(0);
    }
    num -= 2;
  }
  return res;
}

function handleStr(str){
  if(str.length>6){
    str = str.slice(0,6) + "...";
  }
  return str;
}

module.exports = {
  formatTime,
  http,
  handleStar,
  handleStr
}
